package kr.hkit.summary_java.serviceUI;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.uicompent.InputPanel;
import kr.hkit.summary_java.dao.DepartmentDAO;
import kr.hkit.summary_java.erp.dto.Department;

public class PanelDepartmentUI extends JPanel implements ActionListener,FocusListener{
	private JButton btn;
	private InputPanel pDeptno;
	private InputPanel pDeptName;
	private InputPanel pFloor;
	private DepartmentDAO dao;
	private JButton btnClose;
	private Connection con;
	/**
	 * Create the panel.
	 */
	public PanelDepartmentUI() {
		con = ConnectionFactory.getConnection();
		initialize();
	}
	public void setDao(DepartmentDAO dao) {
		this.dao = dao;
	}
	//버튼하나로 여러버튼 생성하기
	public String getBtnAdd() {		return btn.getText();
	}

	public void setBtnAdd(String text) {
		btn.setText(text);
	}
	
	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel_Center = new JPanel();
		add(panel_Center);
		panel_Center.setLayout(new GridLayout(0, 1, 0, 0));
		
		pDeptno = new InputPanel("부서번호");
		panel_Center.add(pDeptno);
		pDeptno.getTf().addFocusListener(this);
		
		pDeptName = new InputPanel("부서명");
		panel_Center.add(pDeptName);
		
		pFloor = new InputPanel("위치");
		panel_Center.add(pFloor);
		
		JPanel panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		btn = new JButton();
		if(btn.equals("검색")){
			panelDisable();
		}
		btn.addActionListener(this);
		panel_Bottom.add(btn);
		
		btnClose = new JButton("닫기");
		btnClose.addActionListener(this);
		panel_Bottom.add(btnClose);
	}
	//버튼하나로 여러개의 버튼 실행 (액션),각각의 메서드를 클릭했을때 맞게 수행함
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("추가")) {
			actionPerformedBtnAdd(e);}
			else if (e.getActionCommand().equals("수정")) {
			actionPerformedBtnUpdate(e);}
			else if(e.getActionCommand().equals("삭제")){
			actionPerformedBtnDelete(e);}
			else if(e.getActionCommand().equals("검색")){
			actionPerformedBtnSelect(e);}
			else if(e.getActionCommand().equals("닫기")){
			actionPerformedBtnClose(e);
			}
	}
	
	
	//추가
	protected void actionPerformedBtnAdd(ActionEvent e) {
		
		dao = new DepartmentDAO(con);
		int dno = Integer.parseInt(pDeptno.getTf().getText().trim());
		String name = pDeptName.getTf().getText();
		int floor = Integer.parseInt(pFloor.getTf().getText());
		
		Department d = new Department(dno, name, floor);
		JOptionPane.showMessageDialog(this, d);
		try {
			dao.insertItem(d);
			JOptionPane.showMessageDialog(this,"입력되었습니다");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,"입력실패 다시 확인하세요.");
		}
	}
	//수정
	private void actionPerformedBtnUpdate(ActionEvent e) {
		dao=new DepartmentDAO(con);
		int dno=Integer.parseInt(pDeptno.getTf().getText().trim());
		String name=pDeptName.getTf().getText();
		int floor=Integer.parseInt(pFloor.getTf().getText());
		
		Department d= new Department(dno, name, floor);
		JOptionPane.showMessageDialog(this, d);
		try{
			dao.updateItemByNo(d);
			JOptionPane.showMessageDialog(this, "수정되었습니다");
			}catch(SQLException e1){
				JOptionPane.showMessageDialog(this, "수정실패 다시 확인하세요.");
		}
		
	}
	//삭제
	private void actionPerformedBtnDelete(ActionEvent e) {
		dao=new DepartmentDAO(con);
		int dno=Integer.parseInt(pDeptno.getTf().getText().trim());
		String name=pDeptName.getTf().getText();
		int floor=Integer.parseInt(pFloor.getTf().getText());
		
		Department d= new Department(dno, name, floor);
		JOptionPane.showMessageDialog(this, d);
		try{
			dao.deleteItemByNo(d);
			JOptionPane.showMessageDialog(this, "삭제되었습니다");
			}catch(SQLException e1){
				JOptionPane.showMessageDialog(this, "삭제실패 다시 확인하세요.");
		}
		
	}
	
	private void actionPerformedBtnSelect(ActionEvent e) {//검색
		searchItem();
	}
	//검색(번호입력시 자동으로 출력)
		@Override
		public void focusGained(FocusEvent e) {
		}
		@Override
		public void focusLost(FocusEvent e) {
			searchItem();
			if(btn.getText().equals("검색")){
				panelEnable();
			}
		}
		private void panelDisable() {
			pDeptno.getTf().setEnabled(true);
			pDeptName.getTf().setEnabled(false);
			pFloor.getTf().setEnabled(false);
		}
		private void panelEnable() {
			pDeptno.getTf().setEnabled(true);
			pDeptName.getTf().setEnabled(false);
			pFloor.getTf().setEnabled(false);
			
		}
		
	private void searchItem(){
		Connection con= ConnectionFactory.getConnection();
		dao = new DepartmentDAO(con);
		
		int no=Integer.parseInt(pDeptno.getTf().getText().trim());
		Department d= new Department();
		d.setDeptNo(no);
		
		try{
			d=dao.searchItem(d);
			pDeptName.setTf(d.getDeptName());
			pFloor.setTf(d.getFloor()+"");
			
			
		}catch(SQLException e){
			e.printStackTrace();
		}catch(NullPointerException e){
			if(!btn.getText().equals("추가"))
				JOptionPane.showMessageDialog(PanelDepartmentUI.this, "해당부서가 없어요");
			else
				JOptionPane.showMessageDialog(PanelDepartmentUI.this, "추가가능");
		}
	}
	protected void actionPerformedBtnClose(ActionEvent e) {//닫기버튼 클릭시 윈도우 창 닫기
		pDeptName.setVisible(false);
		pDeptno.setVisible(false);
		pFloor.setVisible(false);
		btn.setVisible(false);
		btnClose.setVisible(false);
	}
	
}
